package com.pirestupppai.adapter;

import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.pirestupppai.R;
import com.pirestupppai.base.BaseAdapter;
import com.pirestupppai.base.BaseViewHolder;
import com.pirestupppai.bean.TupaiUser;
import com.pirestupppai.utils.ImageLoadUtil;
import com.pirestupppai.widgets.view.AvatarView;
import com.pirestupppai.widgets.view.FollowButton;

import java.util.List;

import static com.pirestupppai.ui.activity.FollowListActivity.FOLLOW_TYPE_MESSAGE;
import static com.pirestupppai.widgets.view.FollowButton.REQUEST_UNFOLLOW;

/**
 * 关注粉丝适配
 *
 * author: xiaoluo
 * date: 2017/6/27 15:03
 */
public class FollowListAdapter extends BaseAdapter<TupaiUser> {

    private List<TupaiUser> mList;
    private Context mContext;
    private int mType;

    public FollowListAdapter(Context context, List<TupaiUser> list, int type) {
        super(list);
        this.mList = list;
        this.mContext = context;
        this.mType = type;
    }

    @Override
    public int getLayoutId() {
        return R.layout.item_follow_list;
    }

    @Override
    protected void bindData(BaseViewHolder holder, int position) {
        AvatarView followAvatar = holder.getView(R.id.follow_avatar);
        TextView nickname = holder.getView(R.id.follow_nickname);
        final TextView fansCount = holder.getView(R.id.fans_count);
        TextView askCount = holder.getView(R.id.ask_count);
        TextView workCount = holder.getView(R.id.work_count);
        LinearLayout infoLayout = holder.getView(R.id.follow_info_layout);
        FollowButton followBtn = holder.getView(R.id.follow_btn);
        TextView signature = holder.getView(R.id.follow_signature);

        final TupaiUser user = mList.get(position);
        ImageLoadUtil.getInstance().loadAvatar(user.getAvatar(), followAvatar);
        followAvatar.setUid(user.getId(), false);
        nickname.setText(user.getNickname());
        fansCount.setText(user.getFollower_count() + "");
        signature.setText(user.getSignature());
//            askCount.setText(user.getAsk_count() + "");
//            workCount.setText(user.getReply_count() + "");

        if (mType == FOLLOW_TYPE_MESSAGE) {
            infoLayout.setVisibility(View.GONE);
            followBtn.setVisibility(View.GONE);
            return;
        } else {
            infoLayout.setVisibility(View.GONE);
            followBtn.setVisibility(View.VISIBLE);
        }

        followBtn.setUser(user);
        followBtn.setOnResultListener(new FollowButton.onResultListener() {
            @Override
            public void onSucceed(int type) {
                if (type == REQUEST_UNFOLLOW) {
                    user.setFollower_count(user.getFollower_count() - 1);
                } else {
                    user.setFollower_count(user.getFollower_count() + 1);
                }
                fansCount.setText(user.getFollower_count() + "");
            }
        });
    }
}
